#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:6_student_management.py.py
# author:张世航
# datetime:2024/11/10 17:29
# software: PyCharm
"""
this is function  description 
"""
# import module your need

# 六  用面向对象,实现一个学生Python成绩管理系统;
#       学生的信息存储在文件中;学生信息的字段有(班级,学号,姓名, Python成绩)
#       实现对学生信息及成绩的增,删,改,查方法;

import json

class StudentManagement:
    def __init__(self, file_path):
        self.file_path = file_path
        self.students = self.load_students()

    def load_students(self):
        """
        从文件中加载学生信息
        """
        try:
            with open(self.file_path, 'r') as file:
                return json.load(file)
        except FileNotFoundError:
            return []

    def save_students(self):
        """
        将学生信息保存到文件中
        """
        with open(self.file_path, 'w') as file:
            json.dump(self.students, file)

    def add_student(self):
        """
        添加学生信息
        """
        class_name = input("请输入班级名称: ")
        student_id = input("请输入学号: ")
        name = input("请输入姓名: ")
        score = int(input("请输入Python成绩: "))
        self.students.append({"class": class_name, "id": student_id, "name": name, "score": score})
        self.save_students()
        print("学生信息已添加。")

    def delete_student(self):
        """
        删除学生信息
        """
        student_id = input("请输入要删除的学号: ")
        self.students = [s for s in self.students if s["id"] != student_id]
        self.save_students()
        print("学生信息已删除。")

    def update_student(self):
        """
        更新学生成绩
        """
        student_id = input("请输入要更新成绩的学号: ")
        new_score = int(input("请输入新的Python成绩: "))
        for student in self.students:
            if student["id"] == student_id:
                student["score"] = new_score
                self.save_students()
                print("学生成绩已更新。")
                return
        print("学生未找到。")

    def search_student(self):
        """
        查找学生信息
        """
        student_id = input("请输入要查找的学号: ")
        for student in self.students:
            if student["id"] == student_id:
                print(f"班级: {student['class']}, 学号: {student['id']}, 姓名: {student['name']}, Python成绩: {student['score']}")
                return
        print("学生未找到。")

    def display_students(self):
        """
        显示所有学生信息
        """
        for student in self.students:
            print(f"班级: {student['class']}, 学号: {student['id']}, 姓名: {student['name']}, Python成绩: {student['score']}")

def main():
    management = StudentManagement("students.json")
    while True:
        print("\n请选择操作:")
        print("1. 添加学生信息")
        print("2. 删除学生信息")
        print("3. 更新学生成绩")
        print("4. 查找学生信息")
        print("5. 显示所有学生信息")
        print("6. 退出")
        choice = input("请输入你的选择: ")
        if choice == '1':
            management.add_student()
        elif choice == '2':
            management.delete_student()
        elif choice == '3':
            management.update_student()
        elif choice == '4':
            management.search_student()
        elif choice == '5':
            management.display_students()
        elif choice == '6':
            break
        else:
            print("无效的选择，请重新输入。")

if __name__ == "__main__":
    main()